package com.sishuok.jmstest.topic;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class TransReceiver2 {
	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.15.111:61616");
		for(int i=0;i<2;i++){
			new MyT(connectionFactory).start();
		}
		
	}
}

class MyT extends Thread{
	private ConnectionFactory connectionFactory;
	public MyT(ConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}
	
	public void run() {
		try {
			final Connection connection = connectionFactory.createConnection();
			connection.start();
			
			final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createTopic("my-topic");
			
			MessageConsumer consumer = session.createConsumer(destination);
			consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					try {
						TextMessage txtMsg = (TextMessage) message;
						System.out.println("收到消息："+txtMsg.getText());
						session.commit();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
	}
}
